package com.egao.gzh.service;

import com.egao.gzh.entity.GzhAccessToken;
import com.egao.gzh.utils.WXPublicConstants;
import net.sf.json.JSONObject;
import org.springframework.web.client.RestTemplate;

/**
 * @author Damon
 * @create 2020-08-27 11:29
 */
public class HtmlToken {

    private static GzhAccessToken at;

    /**
     * 微信公众平台access token
     */
    public static void getToken(){
//        String a = HttpsUtils.get(WXPublicConstants.URL);
        String a = new RestTemplate().getForObject(WXPublicConstants.URL, String.class);
//        System.out.println("aaaa======"+a);
        JSONObject ob = JSONObject.fromObject(a);
        String token2 = ob.getString("access_token");
        String expiresIn = ob.getString("expires_in");
        at = new GzhAccessToken(token2, expiresIn);
    }

    /**
     * 向外暴露的获取token的方法
     */
    public static String getAccessToken() {
        if (at == null || at.isExpired()) {
            getToken();
        }
        return at.getAccessToken();
    }


}
